package com.ruoyi.channel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.channel.domain.bo.TChannelPushDisRecordBo;
import com.ruoyi.channel.domain.vo.TChannelPushDisRecordVo;
import com.ruoyi.channel.domain.TChannelPushDisRecord;
import com.ruoyi.channel.mapper.TChannelPushDisRecordMapper;
import com.ruoyi.channel.service.ITChannelPushDisRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 渠道数据分配记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-02
 */
@RequiredArgsConstructor
@Service
public class TChannelPushDisRecordServiceImpl implements ITChannelPushDisRecordService {

    private final TChannelPushDisRecordMapper baseMapper;

    /**
     * 查询渠道数据分配记录
     */
    @Override
    public TChannelPushDisRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询渠道数据分配记录列表
     */
    @Override
    public TableDataInfo<TChannelPushDisRecordVo> queryPageList(TChannelPushDisRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TChannelPushDisRecord> lqw = buildQueryWrapper(bo);
        Page<TChannelPushDisRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询渠道数据分配记录列表
     */
    @Override
    public List<TChannelPushDisRecordVo> queryList(TChannelPushDisRecordBo bo) {
        LambdaQueryWrapper<TChannelPushDisRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TChannelPushDisRecord> buildQueryWrapper(TChannelPushDisRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TChannelPushDisRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChannelPushId() != null, TChannelPushDisRecord::getChannelPushId, bo.getChannelPushId());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), TChannelPushDisRecord::getTel, bo.getTel());
        lqw.eq(bo.getUserId() != null, TChannelPushDisRecord::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getDataInfo()), TChannelPushDisRecord::getDataInfo, bo.getDataInfo());
        lqw.eq(bo.getCreateId() != null, TChannelPushDisRecord::getCreateId, bo.getCreateId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TChannelPushDisRecord::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增渠道数据分配记录
     */
    @Override
    public Boolean insertByBo(TChannelPushDisRecordBo bo) {
        TChannelPushDisRecord add = BeanUtil.toBean(bo, TChannelPushDisRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改渠道数据分配记录
     */
    @Override
    public Boolean updateByBo(TChannelPushDisRecordBo bo) {
        TChannelPushDisRecord update = BeanUtil.toBean(bo, TChannelPushDisRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TChannelPushDisRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除渠道数据分配记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
